<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .wrap {
      width: 400px;
      height: 400px;
      margin: 100px;
      border: 1px solid #333;
      position: relative;
    }

    .shadow {
      width: 100px;
      height: 100px;
      background-color: rgba(0, 0, 0, 0.5);
      position: absolute;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="shadow"></div>
  </div>
</body>
<script>
  // 需求: 盒子跟随鼠标移动   
  // (1)  鼠标移动 => 怎么知道鼠标在哪里移动(wrap)  移动到了哪个位置?
  // (2)  盒子如何跟随鼠标移动?   margin  translate  position

  var wrap = document.getElementsByClassName("wrap")[0];
  var shadow = document.getElementsByClassName("shadow")[0];

  wrap.onmousemove = function (e) {
    var e = e || window.event;
    var x = e.pageX - wrap.offsetLeft;
    var y = e.pageY - wrap.offsetTop;

    shadow.style.left = x + "px";
    shadow.style.top = y + "px";

  }



</script>

</html>